package com.vrv.netmgr.entry;


import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.vrv.netmgr.R;

/**
 * 导航页面
 * @author yzj
 */
public class GuideActivity extends Activity {
	//定义存放圆点的数组
	private ArrayList<ImageView> viewList;
	//定义viewpager对象
	private ViewPager viewPager;
	//定义viewpager适配器
	private LayoutInflater layoutInflater;
	//定义 导航视图
	private View view1, view2, view3, view4, view5;
	//定义个一个ArrayList<View>存放所有view
	private ArrayList<View> listViews;
	//定义开始监听
	private LinearLayout startButton;
	//定义底部圆点指示
	private ImageView iv1, iv2, iv3, iv4, iv5;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		//初始化页面
		initViews();
	}
	//初始化组件
	public void initViews(){
		//获取视图代理
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater = LayoutInflater.from(this);
		//布局加入视图
		view1 = layoutInflater.inflate(R.layout.activity_guide_1, null);
		view2 = layoutInflater.inflate(R.layout.activity_guide_2, null);
		view3 = layoutInflater.inflate(R.layout.activity_guide_3, null);
		view4 = layoutInflater.inflate(R.layout.activity_guide_4, null);
		view5 = layoutInflater.inflate(R.layout.activity_guide_5, null);
		//获取底部圆点指示
		iv1 = (ImageView) this.findViewById(R.id.iv1);
		iv2 = (ImageView) this.findViewById(R.id.iv2);
		iv3 = (ImageView) this.findViewById(R.id.iv3);
		iv4 = (ImageView) this.findViewById(R.id.iv4);
		iv5 = (ImageView) this.findViewById(R.id.iv5);
		//获取arraylist实例
		viewList = new ArrayList<ImageView>();
		viewList.add(iv1);
		viewList.add(iv2);
		viewList.add(iv3);
		viewList.add(iv4);
		viewList.add(iv5);
		//获取viewpager
		viewPager = (ViewPager) this.findViewById(R.id.viewPager);
		//添加滑动监听
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				matchView(arg0);
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		//获取listViews并存放view
		listViews = new ArrayList<View>();
		listViews.add(view1);
		listViews.add(view2);
		listViews.add(view3);
		listViews.add(view4);
		listViews.add(view5);
		//viewpager实现适配
		PagerAdapter pagerAdapter = new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return listViews.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(listViews.get(position));
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(listViews.get(position));
				return listViews.get(position);
			}
		};
		//viewpager添加适配
		viewPager.setAdapter(pagerAdapter);
		//获取开始按钮
		startButton = (LinearLayout) view5.findViewById(R.id.my_click);
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(GuideActivity.this, LoginActivity.class));
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				GuideActivity.this.finish();
			}
		});
	}
	//页面滑动事件匹配
	public void matchView(int position){
		for(int i=0; i<viewList.size(); i++){
			if(position == i){
				viewList.get(i).setBackgroundResource(R.drawable.guide_point_seleced);
			}else{
				viewList.get(i).setBackgroundResource(R.drawable.guide_point_normal);
			}
		}
	}
}
